//
//  ViewController.m
//  iOS10Notification
//
//  Created by Jim1024 on 2016/12/29.
//  Copyright © 2016年 Jim1024. All rights reserved.
//

#import "ViewController.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import <MobileCoreServices/MobileCoreServices.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor grayColor]];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"toucheBegan.............");
    
    [self createNotification];
    
//    NSFileManager *manager = [NSFileManager defaultManager];
    UIImage *img = [UIImage imageNamed:@"image_11"];
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"testIcon.png"];
    NSURL *docPath = [NSURL fileURLWithPath:path];
    [UIImagePNGRepresentation(img) writeToURL:docPath atomically:YES];
}

// 创建本地通知
- (void)createNotification {
    // 创建通知内容
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"通知标题";
    content.subtitle = @"通知子标题";
    content.body = @"通知的body";
    content.badge = @10;
    
    // 设置通知附件内容
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil];
    NSError *error = nil;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:CFBridgingRelease(kUTTypeGIF) forKey:UNNotificationAttachmentOptionsTypeHintKey];
    [dict setValue:@NO forKey:UNNotificationAttachmentOptionsThumbnailHiddenKey];
//    [dict setValue:CFBridgingRelease(CGRectCreateDictionaryRepresentation(CGRectMake(0.5, 0.5, 0.25, 0.25))) forKey:UNNotificationAttachmentOptionsThumbnailClippingRectKey];
    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:dict error:&error];
    if (error) {
        NSLog(@"attachment error %@", error);
    }
    
    content.attachments = @[attachment];
    content.launchImageName = @"image_11@3x";
    // 设置声音
    UNNotificationSound *sound = [UNNotificationSound defaultSound];
    content.sound = sound;
    
    // 设置触发模式
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
    
    // 设置UNNotificationRequest
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"TestRequest" content:content trigger:trigger];
    
    // 把通知加到UNUserNotificationCenter, 到指定触发点会被触发
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"addNotificationRequest error: %@", error);
    }];
}

@end
